package com.system.service.Impl;

import com.system.dao.ShiroMapper;
import com.system.domain.Permission;
import com.system.domain.User;
import com.system.service.ShiroService;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;

public class ShiroServiceImpl implements ShiroService {

    @Autowired
    private ShiroMapper shiroMapper;

    @Override
    public User getUserByUserName(String username) {
        System.out.println("getUserByUserName......");
        System.out.println(username);
        //根据账号获取账号密码
        //User userByUserName = shiroMapper.getUserByUserName(username);
        return shiroMapper.getUserByUserName(username);
    }

    @Override
    public int delete(String id){
        return shiroMapper.delete(id);
    }

    @Override
    public int updata(User user){
        return shiroMapper.updata(user);
    }

    @Override
    public List<User> getUser() {
        return shiroMapper.getUser();
    }

    @Override
    public List<Permission> getPermissionsByUser(User user) {
        //获取到用户角色userRole
        List<Integer> roleId = shiroMapper.getUserRoleByUserId(user.getId());
        List<Permission> perArrary = new ArrayList<>();

        if (roleId!=null&&roleId.size()!=0) {
            //根据roleid获取peimission
            for (Integer i : roleId) {
                perArrary.addAll(shiroMapper.getPermissionsByRoleId(i));
            }
        }
        System.out.println(perArrary);
        return perArrary;
    }

    @Override
    public int addUser(User user) {
        return shiroMapper.addUser(user);
    }
}
